<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<view class="head" :style="{ paddingTop: navBarHeight }">
			<view class="head-np">
				<view class="back" @click="back">取消</view>
				<!-- {{  $t('__4__') }} 选择联系人 {{ type }}-->
				<view class="title">选择联系人</view>
				<view class="submit" @click="createNewGroup">完成</view>
			</view>


		</view>

		<view class="main">

			<view class="selected-box">
				<view v-for="item in selectUserArr">
					<!-- {{ item }} -->
					<image :src="item.headImage"></image>
				</view>
			</view>

			<view class="select-wrap">
				<scroll-view scroll-y="true" class="scroll-Y">
					<view class="select-ht" v-for="item in firendList" @click="selectUser(item)">
						<view class="slt-circle">
							<image src="../../static/img/chat-08.png" v-if="selectIdList.indexOf(item.id) == -1"></image>
							<image src="../../static/img/chat-07.png" v-else></image>
						</view>
						<view class="slt-info">
							<image :src="item.headImage"></image>
							<text>{{ item.nickName }}</text>
						</view>
						<!-- <image :src="item.url" style="width: 60rpx; height: 60rpx;"></image> -->
					</view>
				</scroll-view>
			</view>
		</view>



	</view>
</template>

<script>
	import { throttle } from '@/utils/common.js'
	import { friendListApi } from '@/api/api.js'
	import navBarMixin from '@/mixin/navBarMixin.js'
	export default {
		data() {
			return {
				type: '', // 1.创建群聊，2.添加好友，3.删除好友
				
				selectUserArr: [], // 选择用户的集合
				selectIdList: [], // 选择用户的id 
				firendList: [],
			}
		},
		mixins: [navBarMixin],
		methods: {
			back() {
				uni.navigateBack()
			},
			
			// 加载好友
			async getFriendList() {
				const res = await friendListApi()
				if (!res.code) {
					this.firendList = res.data
				}
			},

			// 创建群聊
			createNewGroup() {
				throttle(() => {
					if(!this.selectIdList.length) {
						uni.showToast({
							title: '请先选择选联系人',
							icon: 'none'
						})
						return
					}
					
					let userInfo = this.$store.state.userStore.userInfo;
					this.group = {
						// name: `${userInfo.userName}创建的群聊`,
						name: `${userInfo.nickname}创建的群聊`,
						headImage: "https://ktalk-chat.oss-cn-beijing.aliyuncs.com/upload/2024/04/19/1713507924862.png",
						headImageThumb: userInfo.headImageThumb,
						aliasName: userInfo.nickname,
						ownerId: this.$store.state.userStore.userInfo.id
					}
					
					this.$http({
						url: "/group/create",
						method: 'POST',
						data: this.group
					}).then((group) => {
						console.log('group===',group)
						
						this.$store.commit("addGroup", group);
						
						let inviteVo = {
							groupId: group.id,
							friendIds: this.selectIdList
						}
						this.$http({
							url: "/group/invite",
							method: 'POST',
							data: inviteVo
						}).then(() => {
							uni.showToast({
								title: `群聊创建成功`,
								icon: 'none',
							});
							
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/chat/chat'
								})
							},2000)
						})
					
						console.log('group===', group)
					
					})
					
				},4000)
		
			},
			
			// 点击是否选择用户
			selectUser(item) {
				if(this.selectIdList.indexOf(item.id) != -1) {
					this.selectIdList = this.selectIdList.filter(user => user != item.id)
					this.selectUserArr =  this.selectUserArr.filter(iv => iv.id != item.id)
					// console.log('this.selectIdList后===',this.selectIdList)
				}else {
					this.selectUserArr.push(item)
					 this.selectIdList.push(item.id)
				}
				
			}
		},
		onLoad(options) {
			const systemInfo = uni.getSystemInfoSync()
			const height = systemInfo.statusBarHeight;
			const navigationBarHeight = systemInfo.platform === 'android' ? 48 : 48 // 根据不同平台设置导航栏高度 
			const totalHeight = height + navigationBarHeight
			this.topheight = height + 'px';
			this.totalheight = totalHeight + 'px'
			
			this.type = options.type
			this.getFriendList()
		}
	}
</script>

<style lang="scss" scoped>
	.wf-box {
		position: relative;
		overflow: hidden;

		.head {
			position: fixed;
			top: 0;
			z-index: 1;
			width: 100%;
			background: #fff;

			.head-np {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				height: 96rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
			}

			.back {
				color: #333;
				font-size: 28rpx;
			}

			.title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.submit {
				padding: 6rpx 24rpx;
				font-size: 28rpx;
				background: $theme-color-a;
				color: #fff;
				border-radius: 28rpx;
			}
		}

		.main {
			padding: 0 30rpx;
			padding-bottom: 60rpx;
			margin-top: 30rpx;
		}

		.selected-box {
			display: flex;
			min-height: 80rpx;
			// position: fixed;
			flex-wrap: wrap;
			padding: 10rpx 10rpx;
			background-color: #fff;
			border-radius: 6rpx;

			view {
				width: max-content;
				margin: 10rpx;

				image {
					width: 60rpx;
					height: 60rpx;
				}
			}

		}

		.select-wrap {
			height: 1000rpx;
			padding: 30rpx;
			margin-top: 30rpx;
			background: #fff;
			border-radius: 6rpx;
			box-sizing: border-box;

			.scroll-Y {
				height: 970rpx;
			}

			.select-ht {
				display: flex;
				align-items: center;
				padding-top: 30rpx;
				padding-bottom: 30rpx;

				.slt-circle {
					image {
						width: 30rpx;
						height: 30rpx;
					}
				}

				.slt-info {
					position: relative;
					flex: 1;
					display: flex;
					align-items: center;
					margin-left: 30rpx;

					image {
						width: 60rpx;
						height: 60rpx;
					}

					text {
						margin-left: 20rpx;
						color: #333;
					}

					&::after {
						content: '';
						position: absolute;
						width: 100%;
						height: 2rpx;
						background: #eee;
						bottom: -30rpx;
					}
				}

				&:first-child {
					padding-top: 0;
				}
			}
		}

	}
</style>